﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WebShopData;

namespace AdminTools
{
    public partial class OrderOverviewForm : Form
    {
        public OrderOverviewForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            backgroundWorkerLoadOrderHeads_DoWork(sender, null);
        }

        private void addArticleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var articleForm = new ArticleAddForm();
            articleForm.ShowDialog();
            RefreshDataGrid();
        }

        private void dataGridViewAllOrderHeads_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                string orderidstring = dataGridViewAllOrderHeads.Rows[e.RowIndex].Cells[0].Value.ToString();
                int orderId = int.Parse(orderidstring);

                var orderForm = new OrderDetailsForm(orderId);

                orderForm.ShowDialog();
                RefreshDataGrid();
            }
            
        }

        private void RefreshDataGrid()
        {
            backgroundWorkerLoadOrderHeads_DoWork(null, null);
        }

        private void backgroundWorkerLoadOrderHeads_DoWork(object sender, DoWorkEventArgs e)
        {
            var dataset = WebShopDal.GetAllOrderHeads();
            dataGridViewAllOrderHeads.DataSource = dataset.Tables[0];
        }
    }
}
